﻿/****************************** Module Header ******************************\
 * Module Name:  DebugHelper.cs
 * Module Version:  1.0.0 (2014-3-14)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used to debug.
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace UCL
{
    /// <summary>
    /// 调试帮助类
    /// </summary>
    /// <remarks>2014-3-14 by sutroon</remarks>
    public class DebugHelper
    {
        /// <summary>
        /// 调试模式
        /// </summary>
        public enum DebugModes
        {
            /// <summary>
            /// 弹窗显示
            /// </summary>
            WINDOW,
            /// <summary>
            /// 调试窗口
            /// </summary>
            COMMAND,
            /// <summary>
            /// 写入到日志文件
            /// </summary>
            FILE
        }
        /// <summary>
        /// 获取或设置是否开启调试
        /// </summary>
        public static bool On { get; set; }

        /// <summary>
        /// 获取或设置调试模式,默认值SHOW
        /// <para>SHOW=即时输出,LOG=日志输出,ALL=即时输出并写入日志</para>
        /// </summary>
        public static DebugModes DebugMode { get; set; }

        /// <summary>
        /// 日志文件总文件夹
        /// <para>如：E:\AppLog</para>
        /// <para>如果设置此文件夹，则日志文件会自动在文件夹中创建日期子目录,如：E:\AppLog\2014_03_14</para>
        /// </summary>
        public static string LogDir { get; set; }

        /// <summary>
        /// 获取或设置显示消息的委托方法
        /// </summary>
        public static Action<string> ShowMessageHandler { get; set; }

        // 消息集合
        private static IList<string> messageSet;

        /// <summary>
        /// 获取或设置调试启用调试的关键词
        /// <para>如：soc-debug中的-debug</para>
        /// </summary>
        public string[] DebugKeywords { get; set; }

        /// <summary>
        /// 检测字符串中是否含有开启调试模式的关键词
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        /// <remarks>2014-3-18 by sutroon</remarks>
        public bool CheckDebugKeywords(string s)
        {
            if (s.ContainsWords(DebugKeywords)) On = true;
            return On;
        }

        /// <summary>
        /// 记录一条日志消息
        /// </summary>
        /// <param name="s"></param>
        /// <param name="args"></param>
        public static void Log(string s, params object[] args)
        {
            if (messageSet == null) messageSet = new List<string>();
            if (DebugMode == null) DebugMode = DebugModes.WINDOW | DebugModes.FILE;
            if (DebugMode == DebugModes.WINDOW && ShowMessageHandler != null) ShowMessageHandler(FormatLogString(s, args));
            if (DebugMode == DebugModes.FILE) messageSet.Add(FormatLogString(s, args));
        }

        /// <summary>
        /// 格式化日志字符串
        /// <para>默认输出：2014-03-14 15:45:32 The message text...</para>
        /// </summary>
        /// <param name="s"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        private static string FormatLogString(string s, params object[] args)
        {
            if (args.Length > 0) s = string.Format(s, args);
            return string.Format("{0} {1}{2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), s, Environment.NewLine);
        }

        /// <summary>
        /// 输出日志文件
        /// <para>成功则返回SUCCESS,否则返回错误消息</para>
        /// <para>如:Write("F:\log.log");如果不指定文件路径，则系统自动创建日志文件:F:\MyLog\2014_03_14\log_2014_03_14_15.log</para>
        /// </summary>
        /// <param name="filepath"></param>
        /// <returns></returns>
        private static string Write(string filepath = "")
        {
            // 如果名称为空则自动生成文件名,格式 log_2013_11_7_16.log
            if (string.IsNullOrEmpty(filepath))
            {
                if (string.IsNullOrEmpty(LogDir)) return "LogDir is undefined!";
                if (LogDir.EndsWith("\\")) LogDir = LogDir.TrimEnd('\\');
                filepath = string.Format("{1}\\log_{0}.log", LogDir, DateTime.Now.ToString("yyyy_MM_dd_HH"));
            }
            File.AppendAllText(filepath, messageSet.ToString(Environment.NewLine));
            return "SUCCESS";
        }
    }
}
